<?php
namespace App\Modules\Relationship\Models;

use Illuminate\Support\Facades\DB;
use App\Http\Model\Base;

class CoupleKeepsakeModel extends Base {

    protected $table = "rs_couple_keepsake";
    protected $fields = ['id', 'keepsake_name as keepsakeName', 'price', 'upgrade_price as upgradePrice',
        'content', 'head_img_url as headImgUrl', 'rule_img_url as ruleImgUrl', 'decorate_svg as decorateSvg',
        'backgroud_svg as backgroudSvg', 'prop_id as propId', 'level', 'give_prop as giveProp','give_gift as giveGift',
        'give_intimate as giveIntimate', 'gift_upper_limit as giftUpperLimit', 'unlock_ring as unlockRing',
        'is_show as isShow', 'is_floating as isFloating'];
    protected $tablePrefix = '';

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
        $this->tablePrefix = env('DB_PREFIX');
    }

    // 获取最低等级信物
    public function getFirstKeepsake () {
        $re = DB::table($this->table)->orderBy('level','asc')->first();
        if (!$re) {
            return null;
        }
        return self::objToArray($re);
    }

    /**
     * 获取信物 by道具ID
     * @param $propId
     * @return array
     */
    public function getKeepSakeByProp($propId){
        $condition = array(
            "prop_id"=>$propId,
            "is_deleted"=>0
        );
        return $this->getOneByCondition($condition, $this->fields);
    }

    // 信物
    public function getOneKeepsakes ($keepsakeId) : array {
        $condition = array(
            "id"=>$keepsakeId,
            "is_deleted"=>0
        );
        $return = $this->getOneByCondition($condition, $this->fields);
        if (!$return) {
            // TODO 抛异常
            throw new \Exception("信物记录不存在");
        }
        return $return;
    }

    // 查出全部信物
    public function getAllKeepsakes ($keepsakeId = 0) {
        $condition[] = ["is_deleted", 0];

        if ($keepsakeId != 0) {
            $condition[] = ["id", ">=", $keepsakeId];
        }
        $return = $this->getListByCondition($condition, $this->fields, array('field'=>'level', 'sort'=>'asc'));
        return $return;
    }

    // 创建信物
    public function createKeepsake ($data) {
        if (!isset($data['keepsakeName'])) {
            $error = "信物名称不能为空";
        }
        if (!isset($data['level'])) {
            $error = "信物等级不能为空";
        }
        if (!isset($data['price'])) {
            $error = "购买价格不能为空";
        }
        if (!isset($data['upgradePrice'])) {
            $error = "升级价格不能为空";
        }
        if (!isset($data['isShow'])) {
            $error = "信物是否显示不能为空";
        }
        if (!isset($data['propId'])) {
            $error = "信物道具不能为空";
        }
        if (!isset($data['headImgUrl'])) {
            $error = "关联密友头像框不能为空";
        }
        if (!isset($data['ruleImgUrl'])) {
            $error = "信物说明不能为空";
        }
        if (!isset($data['backgroudSvg'])) {
            $error = "信物背景不能为空";
        }
        if (!isset($data['decorateSvg'])) {
            $error = "信物资源不能为空";
        }
        if (!empty($error)) {
            return $error;
        }
        $insert['keepsake_name'] = (string)$data['keepsakeName'];
        $insert['level'] = (int)$data['level'];
        $insert['price'] = (int)$data['price'];
        $insert['upgrade_price'] = (int)$data['upgradePrice'];
        $insert['prop_id'] = (int)$data['propId'];
        $insert['is_show'] = (int)$data['isShow'] ?? 0;
        $insert['head_img_url'] = (string)$data['headImgUrl'];
        $insert['rule_img_url'] = (string)$data['ruleImgUrl'];
        $insert['decorate_svg'] = (string)$data['decorateSvg'];
        $insert['backgroud_svg'] = (string)$data['backgroudSvg'];
        $insert['give_intimate'] = (int)$data['giveIntimate'] ?? 0;
        $insert['gift_upper_limit'] = (int)$data['giftUpperLimit'] ?? 0;
        $insert['is_floating'] = (int)$data['isFloating'] ?? 0;
        $insert['unlock_ring'] = (string)$data['unlockRing'] ?? '';
        $insert['give_prop'] = (string)$data['giveProp'];
        return DB::table($this->table)->insert($insert);
    }

    // 创建信物
    public function updateKeepsake ($data) {
        if ($data['keepsakeName'] !== null) {
            $update['keepsake_name'] = (string)$data['keepsakeName'];
        }
        if ($data['level'] !== null) {
            $update['level'] = (int)$data['level'];
        }
        if ($data['price'] !== null) {
            $update['price'] = (int)$data['price'];
        }
        if ($data['upgradePrice'] !== null) {
            $update['upgrade_price'] = (int)$data['upgradePrice'];
        }
        if ($data['propId'] !== null) {
            $update['prop_id'] = (int)$data['propId'];
        }
        if ($data['isShow'] !== null) {
            $update['is_show'] = (int)$data['isShow'];
        }
        if ($data['headImgUrl'] !== null) {
            $update['head_img_url'] = (string)$data['headImgUrl'];
        }
        if ($data['ruleImgUrl'] !== null) {
            $update['rule_img_url'] = (string)$data['ruleImgUrl'];
        }
        if ($data['decorateSvg'] !== null) {
            $update['decorate_svg'] = (string)$data['decorateSvg'];
        }
        if ($data['backgroudSvg'] !== null) {
            $update['backgroud_svg'] = (string)$data['backgroudSvg'];
        }
        if ($data['giveIntimate'] !== null) {
            $update['give_intimate'] = (int)$data['giveIntimate'];
        }
        if ($data['giftUpperLimit'] !== null) {
            $update['gift_upper_limit'] = (int)$data['giftUpperLimit'];
        }
        if ($data['isFloating'] !== null) {
            $update['is_floating'] = (int)$data['isFloating'];
        }
        if ($data['unlockRing'] !== null) {
            $update['unlock_ring'] = (string)$data['unlockRing'];
        }
        if ($data['giveProp'] !== null) {
            $update['give_prop'] = (string)$data['giveProp'];
        }

        return DB::table($this->table)->where('id', $data['id'])->update($update);
    }

    public function deleteKeepsake ($id) {
        return DB::table($this->table)->where('id',$id)->update(array('is_deleted'=>1));
    }
}

